package org.jeecg.modules.wlhy.tlaccount.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.paycenter.channel.tonglian.TongLianMemberService;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.tlaccount.entity.HyTlAccountRecord;
import org.jeecg.modules.wlhy.tlaccount.service.IHyTlAccountRecordService;
import org.jeecg.modules.wlhy.utils.HyCfgUtil;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

 /**
 * @Description: hy_tl_account_record
 * @Author: jeecg-boot
 * @Date:   2022-10-08
 * @Version: V1.0
 */
@Api(tags="hy_tl_account_record")
@RestController
@RequestMapping("/tlaccount/hyTlAccountRecord")
@Slf4j
public class HyTlAccountRecordController extends JeecgController<HyTlAccountRecord, IHyTlAccountRecordService> {
	@Autowired
	private IHyTlAccountRecordService hyTlAccountRecordService;

	@Autowired
	private TongLianMemberService memberService;

	@Autowired
	private UserUtil userUtil;

	@ApiOperation(value="hy_tl_account_record-分页列表查询", notes="hy_tl_account_record-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HyTlAccountRecord hyTlAccountRecord,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {

	    if(!userUtil.isSys()){
            HyCustomer customer = userUtil.getCustomer();
            hyTlAccountRecord.setUid(customer.getId());
        }
		QueryWrapper<HyTlAccountRecord> queryWrapper = QueryGenerator.initQueryWrapper(hyTlAccountRecord, req.getParameterMap());
		Page<HyTlAccountRecord> page = new Page<HyTlAccountRecord>(pageNo, pageSize);
		IPage<HyTlAccountRecord> pageList = hyTlAccountRecordService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @GetMapping(value = "/checkRecharge")
	 @Transactional
	 public Result<?> checkRecharge(@RequestParam(name="merOrderNo") String merOrderNo){
		 if(StringUtils.isEmpty(merOrderNo)){
			 return Result.error("商户订单号不能为空");
		 }
		 if(!HyCfgUtil.isTongLianUsed()){
			 return Result.error("平台暂未开通服务！");
		 }
		 HyTlAccountRecord record = hyTlAccountRecordService.getByMerOrderNoForUpdate(merOrderNo);
		 if (record == null) {
			 return Result.error("订单不存在");
		 }
		 if(!"1".equals(record.getOptType())){
			 return Result.error("订单类型错误");
		 }
		 if ("1".equals(record.getSt())) {
			 return Result.error("该订单已支付完成");
		 }
		 if ("2".equals(record.getSt())) {
			 return Result.error("该订单支付失败，请重新发起支付");
		 }
		 if(!userUtil.isSys()){
			 HyCustomer customer = userUtil.getCustomer();
			 if(!StringUtils.equals(customer.getId(),record.getUid())){
				 return Result.error("数据错误");
			 }
		 }
		 return memberService.updateOrderNo(record);
	 }

	@AutoLog(value = "hy_tl_account_record-通过id删除")
	@ApiOperation(value="hy_tl_account_record-通过id删除", notes="hy_tl_account_record-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		HyTlAccountRecord record = hyTlAccountRecordService.getById(id);
		if(record == null){
			return Result.error("记录不存在");
		}
		if(!userUtil.isSys()){
			HyCustomer customer = userUtil.getCustomer();
			if(!StringUtils.equals(customer.getId(),record.getUid())){
				return Result.error("记录不存在");
			}
		}
//		if(!"2".equals(record.getSt()) && !"4".equals(record.getSt())){
		if(!"2".equals(record.getSt())){
			return Result.error("该记录不能删除");
		}
		hyTlAccountRecordService.removeById(id);
		return Result.OK("删除成功!");
	}

}
